package com.polonium.eclipserunner.core;

/** Integration idea - try of making AbstractTransactionalJUnitTests part of PoloniumTests
 * @author Marek Serwanski
 *
 */

public class PoloniumTransactionalSpringTestRunner {//extends SpringJUnit4ClassRunner {

//	public static boolean DETAILED_DESCRIPTION = false;
//
//	private TestClass testInstance;
//	protected TransactionalSpringDescriptionBuilder descriptionBuilder;
//	protected PoloniumNotifier notifier;
//	protected ExceptionsRecognizer exceptionsRecognizer;
//	private List<FrameworkMethod> tests;
//
//	/** Creates necessary intances and build arrays of methods in test class 
//	 * @throws InitializationError */
//	public PoloniumTransactionalSpringTestRunner(Class<?> testClass) throws InitializationError {
//		super(testClass);
//		testInstance = super.getTestClass();
//		//exceptionsRecognizer = new ExceptionsRecognizer(testInstance);
//		descriptionBuilder = new TransactionalSpringDescriptionBuilder();
//		notifier = new PoloniumNotifier();
//
//		tests = testInstance.getAnnotatedMethods(Test.class);
//		descriptionBuilder.getDetailedDescriptionType(testInstance);
//		descriptionBuilder.createDescription(testInstance, tests);
//	}
//
//	@Override
//	public Description getDescription() {
//		return descriptionBuilder.getClassDescription();
//	}
//
//	/** Start methods iteration and invokes */
//	@Override
//	public void run(RunNotifier runNotifier) {
//		notifier.setRunNotifier(runNotifier);
//		runMethods();
//	}
//	
//	/** returns proper number of tests (not counting given/when/then descriptions as tests */ 
//	@Override
//	public int testCount() {
//		return tests.size();
//	}
//	
//	/** Flow: <br />
//	 * 1. Invoke all methods annotated @Before <br />
//	 * 2. Iterate all methods annotated @Test : <br />
//	 * 2a. check if properly anntotated with @Given@When@Then <br />
//	 * 2b. invoke <br />
//	 * 2c. if exception occurs, then make proper notifications after exception recognizing <br />
//	 * 2d. if no exception occurs, then notify with ok whole test description <br />
//	 * 3. Invoke all after methods 
//	 */
//	protected void runMethods(){
//		invokeMethods(BeforeClass.class);
//		
//		for (FrameworkMethod test : tests) {
//			int testIndex = tests.indexOf(test);
//			Description testDescription = descriptionBuilder.getTestRootDescription(testIndex);
//
//			if (isReadyToRun(test, testDescription)) {
//				try {
//					// begin test running
//					notifier.setStarted(testDescription);
//					invokeMethods(Before.class);
//
//					// run test - if no errors occurs then test is notified as
//					// OK
//					
//					test.invokeExplosively(testInstance);
//					notifier.setOK(testDescription);
//
//				} catch (Throwable e) {
//					notifier.checkException(testDescription, e);
//				} finally {
//					invokeMethods(After.class);
//				}
//			}
//		}
//	}
//
//	// checks if test is properly annotated and not set as Ignored
//	private boolean isReadyToRun(FrameworkMethod test, Description testDescription) {
//		if (!descriptionBuilder.isTestProperlyAnnotated(test)) {
//			notifier.setFailure(testDescription, new AnnotationException());
//			return false;
//		} else if (test.getAnnotation(Ignore.class) != null) {
//			notifier.setIgnore(testDescription);
//			return false;
//		} else {
//			return true;
//		}
//	}
//
//	private void invokeMethods(Class<? extends Annotation> annotationType) {
//		try {
//			for (FrameworkMethod method : testInstance.getAnnotatedMethods(annotationType)) {
//				method.invokeExplosively(testInstance);
//			}
//		} catch (Throwable e) {
//			System.err.println("Error while invoking " + annotationType.getName() + " methods");
//		}
//	}
//
//	private PoloniumTest getTestInstance(Class<? extends PoloniumTest> testClass) {
//		try {
//			return testClass.newInstance();
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		}
//	}
}
